package com.cskaoyan.wx.service;

import com.cskaoyan.bean.Address;
import com.cskaoyan.bean.AddressExample;
import com.cskaoyan.mapper.AdMapper;
import com.cskaoyan.mapper.AddressMapper;
import com.cskaoyan.mapper.ReceiveAddressMapper;

import com.cskaoyan.utils.UserUtils;
import com.cskaoyan.wx.bo.AddressSaveBO;
import com.cskaoyan.wx.vo.AddressRegionVO;
import com.cskaoyan.wx.vo.ReceiveAddressDetailVO;
import com.cskaoyan.wx.vo.ReceiveAddressTempVO;
import com.cskaoyan.wx.vo.ReceiveAddressVO2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class WxAddressServiceImpl implements WxAddressService {

    @Autowired
    ReceiveAddressMapper receiveAddressMapper;

    @Autowired
    AddressMapper addressMapper;

    @Override
    public List<ReceiveAddressVO2> getAddressList() {


        Integer userId = UserUtils.getUserId();
        List<ReceiveAddressTempVO> receiveAddressTempVOS = receiveAddressMapper.getAddressList(userId);

       List<ReceiveAddressVO2> receiveAddressVO2s = new ArrayList<>();
        for (ReceiveAddressTempVO receiveAddressTempVO : receiveAddressTempVOS) {
            ReceiveAddressVO2 receiveAddressVO2 = new ReceiveAddressVO2();

            receiveAddressVO2.setIsDefault(receiveAddressTempVO.getIsDefault());
            receiveAddressVO2.setDetailedAddress(receiveAddressTempVO.getProvince() + receiveAddressTempVO.getCity() + receiveAddressTempVO.getArea() + " " + receiveAddressTempVO.getAddress());
            receiveAddressVO2.setName(receiveAddressTempVO.getName());
            receiveAddressVO2.setMobile(receiveAddressTempVO.getMobile());
            receiveAddressVO2.setId(receiveAddressTempVO.getId());

            receiveAddressVO2s.add(receiveAddressVO2);
        }
        return receiveAddressVO2s;
    }

    @Override
    public ReceiveAddressDetailVO getAddressDetail(Integer id) {
        ReceiveAddressDetailVO receiveAddressDetailVO = receiveAddressMapper.getAddressDetail(id);
        return receiveAddressDetailVO;
    }
    //添加地址
    @Override
    public void getAddressSave(AddressSaveBO addressSaveBO) {

        Integer id = addressSaveBO.getId();
        Address address = new Address();
        address.setIsDefault(addressSaveBO.getIsDefault());
        address.setAreaId(addressSaveBO.getAreaId());
        address.setAddress(addressSaveBO.getAddress());
        address.setCityId(addressSaveBO.getCityId());
        address.setName(addressSaveBO.getName());
        address.setMobile(addressSaveBO.getMobile());
        address.setProvinceId(addressSaveBO.getProvinceId());
        address.setDeleted(false);
        address.setUserId(UserUtils.getUserId());

        if (addressSaveBO.getIsDefault() ) {//为默认
            AddressExample addressExample = new AddressExample();
            AddressExample.Criteria criteria = addressExample.createCriteria();
            criteria.andUserIdEqualTo(UserUtils.getUserId());
            Address address1 = new Address();
            address1.setIsDefault(false);
            addressMapper.updateByExampleSelective(address1,addressExample);
        }

        if (id == 0) {// 新增一个地址
            address.setAddTime(new Date());
            address.setUpdateTime(new Date());
            addressMapper.insertSelective(address);
        } else {//修改地址
            address.setUpdateTime(new Date());
            AddressExample addressExample = new AddressExample();
            AddressExample.Criteria criteria = addressExample.createCriteria();
          criteria.andIdEqualTo(addressSaveBO.getId());
          criteria.andUserIdEqualTo(UserUtils.getUserId());
          addressMapper.updateByExampleSelective(address,addressExample);
        }
    }

    @Override
    public List<AddressRegionVO> getRegionList(Integer pid) {
        List<AddressRegionVO> addressRegionVOS = receiveAddressMapper.getRegionList(pid);
        return addressRegionVOS;
    }

    @Override
    public void deleteAddress(Integer id) {
        receiveAddressMapper.deleteAddress(id);
    }
}
